package com.example.allinonemanager.moudles.sys.controller;

import com.example.allinonemanager.base.response.JsonResult;
import com.example.allinonemanager.base.response.StatusCode;
import com.example.allinonemanager.moudles.sys.pojo.entity.UserEntity;
import com.example.allinonemanager.moudles.sys.pojo.req.UserLoginReq;
import com.example.allinonemanager.moudles.sys.pojo.req.UserRegisterReq;
import com.example.allinonemanager.moudles.sys.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.example.allinonemanager.base.response.StatusCode.REGISTER_SUCCESS;

@Api(tags = "1.1登录模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class SysLoginController {

    @Autowired
    private UserLoginService userLoginService;

    @ApiOperation("注册功能")
    @PostMapping("/register")
    public JsonResult register(@RequestBody UserRegisterReq userRegisterReq){
        log.debug("注册用户,userRegisterReq={}",userRegisterReq);
        userLoginService.register(userRegisterReq);
        return new JsonResult(StatusCode.REGISTER_SUCCESS);
    }

    @ApiOperation("登录功能")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginReq userLoginReq){
        log.debug("用户登录,userLoginReq={}",userLoginReq);
        UserEntity userEntity = userLoginService.login(userLoginReq);
        return new JsonResult(StatusCode.LOGIN_SUCCESS, userEntity);
    }
}
